//
//  TalkVideoPlayView.m
//  talkDemo
//
//  Created by telen on 15/4/22.
//  Copyright (c) 2015年 刘赞黄Telen. All rights reserved.
//

#import "TalkVideoPlayView.h"
#import <AVFoundation/AVFoundation.h>
#import "TalkDefine.h"

@interface TLPlayerView : UIView
@property (nonatomic ,strong) AVPlayer *player;
@end

@implementation TLPlayerView

+ (Class)layerClass {
    return [AVPlayerLayer class];
}

- (AVPlayer *)player {
    return [(AVPlayerLayer *)[self layer] player];
}
- (void)setPlayer:(AVPlayer *)player {
    [(AVPlayerLayer *)[self layer] setPlayer:player];
}

@end


@implementation TalkVideoPlayView
{
    AVAudioPlayer* audio;
    AVPlayerItem* playItem;
    TLPlayerView* avView;
    NSString* sign;
}

+ (TalkVideoPlayView *)talkVedioPlayView_withSize:(CGSize)size
{
    CGRect frame = CGRectMake(0, 0, 0,0);
    frame.size = size;
    return [[TalkVideoPlayView alloc] initWithFrame:frame];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        UIImage* image = [UIImage imageNamed:@"talk_pop.png"];
        image = [image stretchableImageWithLeftCapWidth:image.size.width*0.7 topCapHeight:image.size.height*0.5];
        UIImageView* igv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        igv.image = image;
        igv.alpha = 0.35;
        [self addSubview:igv];
        //
        CGFloat ratio = [TalkViewController shareLastInstance].ratio;
        CGFloat wpod = ceil(6/ratio);
        CGFloat hpod = ceil(8/ratio);
        avView = [[TLPlayerView alloc] initWithFrame:CGRectMake(wpod, hpod, frame.size.width-wpod*2,frame.size.height-hpod*2-18/ratio)];
        avView.backgroundColor = [UIColor clearColor];
        [self addSubview:avView];
        //
        self.layer.anchorPoint = CGPointMake(0.25, 1);
    }
    return self;
}

- (void)show
{
    self.hidden = NO;
    self.transform = CGAffineTransformMakeScale(1,0.001f);
    [UIView animateWithDuration:0.3f animations:^{
        self.transform = CGAffineTransformMakeScale(1, 1);
    }];
    
    self.isShown = YES;
}

- (void)unshow
{
    [self cleanPlayer];
    [UIView animateWithDuration:0.3f animations:^{
        self.transform = CGAffineTransformMakeScale(1,0.001f);
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
    
    self.isShown = NO;
}

- (void)dealloc
{
    [self cleanPlayer];
}

- (void)cleanPlayer
{
    if (audio) {
        [audio stop],audio  =nil;
    }
    avView.player = nil;
    if (playItem) {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        [playItem removeObserver:self forKeyPath:@"status"];
        [playItem removeObserver:self forKeyPath:@"loadedTimeRanges"];
        playItem = nil;
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    //TLDebugS(@"key:%@   %@",keyPath,change);
}

- (void)AVPlayerItemDidPlayToEndTime:(NSNotification*)notice
{
    //TLDebugS(@"%@",notice);
    [self unshow];
    if (_delegate && [_delegate respondsToSelector:@selector(TalkVedioPlay_DidPlayEnd_sign:)]) {
        [_delegate TalkVedioPlay_DidPlayEnd_sign:sign];
    }
}

- (void)playMp4:(NSString *)mp4Path andMp3:(NSString *)mp3Path sign:(NSString *)sig
{
    sign = sig;
    [self show];
    [self cleanPlayer];
    if (mp3Path) {
        audio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:mp3Path] error:nil];
        [audio performSelector:@selector(play) withObject:nil afterDelay:0.2];
    }
    if (mp4Path) {
        playItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:mp4Path]];
        [playItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
        [playItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(AVPlayerItemDidPlayToEndTime:) name:AVPlayerItemDidPlayToEndTimeNotification object:playItem];
        avView.player = [AVPlayer playerWithPlayerItem:playItem];
        [avView.player performSelector:@selector(play) withObject:nil afterDelay:0.2];
    }
}

@end
